package com.yoomiito.app.ui.anyview.goodsinfo.header

import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.LinearLayout
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.goodsPrice
import com.yoomiito.app.isEmptyOr0
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.removeDecimalPoint
import com.yoomiito.app.ui.goods.NewGoodsInfoActivity
import com.yoomiito.app.utils.*
import kotlinx.android.synthetic.main.item_coupon_view.view.*
import kotlinx.android.synthetic.main.item_goods_detail_price_2.view.*
import kotlinx.android.synthetic.main.item_new_coupon_view.view.*
import kotlinx.android.synthetic.main.item_store.view.*
import kotlinx.android.synthetic.main.item_upgrade_vip.view.*
import kotlinx.android.synthetic.main.video_play_view.view.*
import kotlinx.android.synthetic.main.view_goodsinfo_header.view.*
import java.math.BigDecimal

class TaoBaoGoodsInfoHeaderView(context: BaseActivity<*>):BaseGoodsInfoHeaderView(context){

    override fun getParentView(): LinearLayout = this

    override fun getLayoutId(): Int = R.layout.view_goodsinfo_header

    override fun initView(goodsDetail: GoodsDetail) {
        initBanner(banner, goodsDetail)
        getVideoTime(goodsDetail) {
            playVideoView.visibility = View.VISIBLE
            durationTv.text = DateUtils.ms2min(it.duration)
            playVideoView.setOnClickListener {_->
                OnVideoClickListener(it)
            }

        }
        //价格
        if (viewStubIsInflate(R.id.priceLlVs)){
            priceLlVs.layoutResource = R.layout.item_goods_detail_price_2
            priceLlVs.inflate()
        }
        goodsDesTv.visibility = View.GONE
        val priceT = goodsDetail.salePrice.removeDecimalPoint()
        val priceStr = if (goodsDetail.reserve_price.isEmptyOr0()||goodsDetail.reserve_price==goodsDetail.salePrice) "¥${priceT} " else "¥${priceT} ¥${goodsDetail.reserve_price.removeDecimalPoint()}"
        price.text = priceStr.goodsPrice(priceT,true,R.color.color_balk_999999,0.45f)

        //赚 券
        //赚显示规则：未登录不显示；会员 粉丝及普通VIP显示后台配置的基础佣金；VIP及VIP以上显示【7*佣金倍数】
        awardView.visibility = if (!App.mAppConfig.isLogin ||UserInfoConfig.getRoleId()<Type.LevelType.VIP_7 || SpannableUtils.isEmptyOr0(goodsDetail.get_money)) View.GONE else View.VISIBLE
        awardView.setText("¥${goodsDetail.get_money.removeDecimalPoint()}")
        //优惠券 有就显示
        couponView.setHeaderText("券")
        couponView.setText("¥${goodsDetail.coupon_price.removeDecimalPoint()}")
        couponView.visibility = if (!App.mAppConfig.isLogin ||goodsDetail.coupon_price.isEmptyOr0()) View.GONE else View.VISIBLE

        //升级VIP
        if (!App.mAppConfig.isLogin or (UserInfoConfig.getRoleId() < Type.LevelType.VIP_7)){
            if (viewStubIsInflate(R.id.upgradeVipVs)){
                val view = upgradeVipVs.inflate()
                val lp1 = view.layoutParams as LinearLayout.LayoutParams
                lp1.topMargin = DensityUtil.dp2px(12f)
            }
//            VIPAwardTv.text = "¥${goodsDetail.get_money.toBigDecimal().multiply("0.55".toBigDecimal()).setScale(2, BigDecimal.ROUND_HALF_UP).toString()}"
            VIPAwardTv.text = "¥${goodsDetail.upgrade_money}"
            upgradeVip.setOnClickListener { RouterUtils.toMain(context,1) }
        }

        //标题
        goodsTitleTv.text = SpannableUtils.getIconAndText(goodsDetail.title,ResUtil.getDrawable(DataUtils.ivType(goodsDetail.user_type)))
        goodsTitleCopy(goodsTitleTv,goodsDetail.title)
        goodsTitleTv.setOnClickListener {
            TouchUtils.onEvent(AllEventID.TaoBaoShareEvnetId, UserInfoConfig.getRoleId(), UserInfoConfig.getAgent())
            if (App.mAppConfig.isLogin) {
                RouterUtils.toShareGoods(context, goodsDetail.num_iid.toString(), Type.GoodsType.TaoBao)
            } else {
                LoginUtils.toOneKeyLogin(context)
            }
        }

        //销量 及分享件数
        val size = "月销${goodsDetail.volume}件"
        val sizeSpannableString = SpannableString(size)
        sizeSpannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_333333)), 2, size.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//销量颜色
        goodsSaleSizeTv.text = sizeSpannableString
        goodsShareSizeTv.text=""//不显示分享次数
//        val shareSize = "分享${goodsDetail.share_num}次"
//        val shareSizeSpannableString = SpannableString(shareSize)
//        shareSizeSpannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_333333)), 2, shareSize.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//分享件数颜色
//        goodsShareSizeTv.text = shareSizeSpannableString

        //优惠券
        if(!goodsDetail.coupon_price.isEmptyOr0()){
            if (viewStubIsInflate(R.id.itemCouponView)){
                itemCouponView.inflate()
            }
            couponPriceTv.text = goodsDetail.coupon_price.removeDecimalPoint()
            couponRule.text = "无门槛"
            couponDateTv.text = "使用期限：${goodsDetail.coupon_start_time.replace("-".toRegex(), ".")}-${goodsDetail.coupon_end_time.replace("-".toRegex(), ".")}"
            couponBuy.setOnClickListener {
                if (App.mAppConfig.isLogin) {
                    RouterUtils.TaoBaoWithUrl(goodsDetail.coupon_click_url)
                } else {
//                    AllUtils.toLogin(context, AllCode.LoginIn)
                    LoginUtils.toOneKeyLogin(context)
                }
            }
        }

        if (viewStubIsInflate(R.id.itemGoodsVs)){
            itemGoodsVs.layoutResource = R.layout.item_store
            itemGoodsVs.inflate()
        }
        ImageLoader.getInstance().into(context,goodsDetail.shop_logo.pict_url,item_store_logo,R.drawable.default_shop_logo)
        item_store_name.text = goodsDetail.nick.replace(" ", "\n")
        item_store_flag.setImageResource(DataUtils.storeType(goodsDetail.user_type))

    }
}